{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true,
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "# 4 Pandas的索引操作"
   ]
  },
  {
   "cell_type": "code",
   "source": [
    "import pandas as pd\n",
    "import numpy as np"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-08T12:16:17.324455Z",
     "start_time": "2025-01-08T12:16:16.862983Z"
    }
   },
   "outputs": [],
   "execution_count": 3
  },
  {
   "cell_type": "code",
   "source": [
    "dict_data = {'A': 1,\n",
    "             'B': pd.Timestamp('20190926'),\n",
    "             'C': pd.Series(1, index=list(range(4)), dtype='float32'),\n",
    "             'D': np.array([1, 2, 3, 4], dtype='int32'),\n",
    "             'E': [\"Python\", \"Java\", \"C++\", \"C\"],\n",
    "             'F': 'wangdao'}\n",
    "df_obj2 = pd.DataFrame(dict_data)\n",
    "print(df_obj2)\n",
    "print(df_obj2.index)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-01-07T11:46:47.377407Z",
     "start_time": "2025-01-07T11:46:47.371090Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   A          B    C  D       E        F\n",
      "0  1 2019-09-26  1.0  1  Python  wangdao\n",
      "1  1 2019-09-26  1.0  2    Java  wangdao\n",
      "2  1 2019-09-26  1.0  3     C++  wangdao\n",
      "3  1 2019-09-26  1.0  4       C  wangdao\n",
      "Index([0, 1, 2, 3], dtype='int64')\n"
     ]
    }
   ],
   "execution_count": 5
  },
  {
   "cell_type": "code",
   "source": [
    "# 索引对象的值不可变（上面代码增加）\n",
    "# df_obj2.index[0] = 2"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-01-07T11:46:50.937251Z",
     "start_time": "2025-01-07T11:46:50.934389Z"
    }
   },
   "outputs": [],
   "execution_count": 6
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 3 常见的Index种类\n",
    "•Index，索引  可以是各种类型\n",
    "•Int64Index，整数索引\n",
    "•MultiIndex，层级索引，难度较大\n",
    "•DatetimeIndex，时间戳类型"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "source": [
    "ser_obj = pd.Series(range(5), index=list(\"abcde\"))\n",
    "print(ser_obj)\n",
    "ser_obj.index"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-01-08T12:16:20.085716Z",
     "start_time": "2025-01-08T12:16:19.912455Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a    0\n",
      "b    1\n",
      "c    2\n",
      "d    3\n",
      "e    4\n",
      "dtype: int64\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "Index(['a', 'b', 'c', 'd', 'e'], dtype='object')"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 4
  },
  {
   "cell_type": "code",
   "source": [
    "# 行索引，不仅可以用索引名，可以用索引位置或来取\n",
    "print(ser_obj['b'])  #索引名\n",
    "print(ser_obj[2])  #位置索引"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-01-07T12:13:07.703880Z",
     "start_time": "2025-01-07T12:13:07.699359Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\54439\\AppData\\Local\\Temp\\ipykernel_15232\\1302140613.py:3: FutureWarning: Series.__getitem__ treating keys as positions is deprecated. In a future version, integer keys will always be treated as labels (consistent with DataFrame behavior). To access a value by position, use `ser.iloc[pos]`\n",
      "  print(ser_obj[2])  #位置索引\n"
     ]
    }
   ],
   "execution_count": 3
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T12:13:10.058947Z",
     "start_time": "2025-01-07T12:13:10.054693Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# iloc:索引  loc:索引名\n",
    "print(ser_obj.loc['b'])  #索引名\n",
    "print(ser_obj.iloc[2])  #位置索引"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n"
     ]
    }
   ],
   "execution_count": 4
  },
  {
   "cell_type": "code",
   "source": [
    "# 切片索引  iloc:索引  loc:索引名\n",
    "print(ser_obj.iloc[1:3])  #索引位置取数据，左闭右开\n",
    "print(ser_obj.loc['b':'d'])  #记住索引名  左闭右闭"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-01-07T12:13:11.721966Z",
     "start_time": "2025-01-07T12:13:11.715272Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "b    1\n",
      "c    2\n",
      "dtype: int64\n",
      "b    1\n",
      "c    2\n",
      "d    3\n",
      "dtype: int64\n"
     ]
    }
   ],
   "execution_count": 5
  },
  {
   "cell_type": "code",
   "source": [
    "# 不连续索引\n",
    "print(ser_obj.iloc[[0, 2, 4]])\n",
    "print(ser_obj.loc[['a', 'e']])"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-01-07T12:13:14.669213Z",
     "start_time": "2025-01-07T12:13:14.664002Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a    0\n",
      "c    2\n",
      "e    4\n",
      "dtype: int64\n",
      "a    0\n",
      "e    4\n",
      "dtype: int64\n"
     ]
    }
   ],
   "execution_count": 6
  },
  {
   "cell_type": "code",
   "source": [
    "# 布尔索引\n",
    "ser_bool = ser_obj > 2\n",
    "print(ser_obj)\n",
    "print(ser_bool)\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-01-07T12:13:16.935046Z",
     "start_time": "2025-01-07T12:13:16.930066Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a    0\n",
      "b    1\n",
      "c    2\n",
      "d    3\n",
      "e    4\n",
      "dtype: int64\n",
      "a    False\n",
      "b    False\n",
      "c    False\n",
      "d     True\n",
      "e     True\n",
      "dtype: bool\n"
     ]
    }
   ],
   "execution_count": 7
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T12:13:18.876436Z",
     "start_time": "2025-01-07T12:13:18.871482Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print('-' * 50)\n",
    "print(ser_obj[ser_bool])  #把True的结果筛选出来\n",
    "\n",
    "print(ser_obj[ser_obj > 2])  #取出大于2的元素"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "--------------------------------------------------\n",
      "d    3\n",
      "e    4\n",
      "dtype: int64\n",
      "d    3\n",
      "e    4\n",
      "dtype: int64\n"
     ]
    }
   ],
   "execution_count": 8
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 4.4 DataFrame索引"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "source": [
    "import numpy as np\n",
    "\n",
    "df_obj = pd.DataFrame(np.random.randn(5, 4),\n",
    "                      columns=['a', 'b', 'c', 'd'])\n",
    "print(df_obj.head())"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-01-07T12:13:20.561429Z",
     "start_time": "2025-01-07T12:13:20.555358Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          a         b         c         d\n",
      "0  0.060493  0.493114  0.171063 -1.292946\n",
      "1  0.204586 -0.541370  0.781596  2.589003\n",
      "2 -1.282031  1.308156 -0.644539  0.409075\n",
      "3  0.089624 -1.510933 -1.386934  0.421527\n",
      "4  1.794878  1.357708 -0.436315  0.525011\n"
     ]
    }
   ],
   "execution_count": 9
  },
  {
   "cell_type": "code",
   "source": [
    "# 列索引  不加[]返回Series类型，加[]返回DataFrame类型\n",
    "print(df_obj['a'])  # 返回Series类型\n",
    "print('-' * 50)\n",
    "print(df_obj[['a']])  # 返回DataFrame类型\n",
    "print('-' * 50)\n",
    "print(type(df_obj[['a']]))  # 返回DataFrame类型"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-01-07T12:13:23.594375Z",
     "start_time": "2025-01-07T12:13:23.588708Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    0.060493\n",
      "1    0.204586\n",
      "2   -1.282031\n",
      "3    0.089624\n",
      "4    1.794878\n",
      "Name: a, dtype: float64\n",
      "--------------------------------------------------\n",
      "          a\n",
      "0  0.060493\n",
      "1  0.204586\n",
      "2 -1.282031\n",
      "3  0.089624\n",
      "4  1.794878\n",
      "--------------------------------------------------\n",
      "<class 'pandas.core.frame.DataFrame'>\n"
     ]
    }
   ],
   "execution_count": 10
  },
  {
   "cell_type": "markdown",
   "source": [
    "1. loc 标签索引(通过索引标签值获取数据)"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "source": [
    "# 标签索引 loc，建议使用loc，效率更高\n",
    "# Series\n",
    "print(ser_obj)\n",
    "print(ser_obj['b':'d'])\n",
    "print(ser_obj.loc['b':'d'])  #前闭后闭\n",
    "print('-' * 50)\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-01-07T12:13:26.002337Z",
     "start_time": "2025-01-07T12:13:25.997089Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a    0\n",
      "b    1\n",
      "c    2\n",
      "d    3\n",
      "e    4\n",
      "dtype: int64\n",
      "b    1\n",
      "c    2\n",
      "d    3\n",
      "dtype: int64\n",
      "b    1\n",
      "c    2\n",
      "d    3\n",
      "dtype: int64\n",
      "--------------------------------------------------\n"
     ]
    }
   ],
   "execution_count": 11
  },
  {
   "cell_type": "code",
   "source": [
    "# DataFrame  不加loc拿的是列，加loc拿的是行\n",
    "df_obj = pd.DataFrame(np.random.randn(5, 4),columns=list('abcd'),index=list('abcde'))\n",
    "print(df_obj)\n",
    "print('-' * 50)\n",
    "print(df_obj['a'])  #建议不用,拿的是列\n",
    "print('-' * 50)\n",
    "print(df_obj.loc['a'])  #拿的是行\n",
    "print('-' * 50)\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-07T12:13:31.063138Z",
     "start_time": "2025-01-07T12:13:31.056339Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          a         b         c         d\n",
      "a  0.279237  1.561592 -0.168224 -1.839025\n",
      "b  1.069509  0.094385 -0.121773 -0.486491\n",
      "c  0.221530 -1.933896  1.088034 -0.098264\n",
      "d -0.929662 -0.100557 -1.776573 -0.671149\n",
      "e -0.213642 -0.635774  0.792030 -1.378112\n",
      "--------------------------------------------------\n",
      "a    0.279237\n",
      "b    1.069509\n",
      "c    0.221530\n",
      "d   -0.929662\n",
      "e   -0.213642\n",
      "Name: a, dtype: float64\n",
      "--------------------------------------------------\n",
      "a    0.279237\n",
      "b    1.561592\n",
      "c   -0.168224\n",
      "d   -1.839025\n",
      "Name: a, dtype: float64\n",
      "--------------------------------------------------\n"
     ]
    }
   ],
   "execution_count": 12
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T12:13:59.082318Z",
     "start_time": "2025-01-07T12:13:59.073541Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 第一个参数索引行，第二个参数是列,loc或者iloc效率高于直接用取下标的方式，前闭后闭\n",
    "print(df_obj.loc['a':'c', 'b':'d'])  #连续索引\n",
    "print(df_obj.loc[['a', 'c'], ['b', 'd']])  #不连续索引\n",
    "print(df_obj.loc[['c'], ['b']])  #取一个值,返回的是DataFrame类型\n",
    "print(df_obj.loc['c', 'b'])  #取一个值，不加[]返回的是一个浮点数"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          b         c         d\n",
      "a  1.561592 -0.168224 -1.839025\n",
      "b  0.094385 -0.121773 -0.486491\n",
      "c -1.933896  1.088034 -0.098264\n",
      "          b         d\n",
      "a  1.561592 -1.839025\n",
      "c -1.933896 -0.098264\n",
      "          b\n",
      "c -1.933896\n",
      "-1.9338960580479656\n"
     ]
    }
   ],
   "execution_count": 14
  },
  {
   "cell_type": "markdown",
   "source": [
    "## iloc 位置索引(推荐使用)"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "source": [
    "print(ser_obj)\n",
    "print('-' * 50)\n",
    "# Series\n",
    "print(ser_obj[1:3])\n",
    "print('-' * 50)\n",
    "print(ser_obj.iloc[1:3])  # 前闭后开[)，效率高\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-08T12:16:45.531245Z",
     "start_time": "2025-01-08T12:16:45.526635Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a    0\n",
      "b    1\n",
      "c    2\n",
      "d    3\n",
      "e    4\n",
      "dtype: int64\n",
      "--------------------------------------------------\n",
      "b    1\n",
      "c    2\n",
      "dtype: int64\n",
      "--------------------------------------------------\n",
      "b    1\n",
      "c    2\n",
      "dtype: int64\n"
     ]
    }
   ],
   "execution_count": 6
  },
  {
   "cell_type": "code",
   "source": [
    "df_obj"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-07T12:14:05.716144Z",
     "start_time": "2025-01-07T12:14:05.707555Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "          a         b         c         d\n",
       "a  0.279237  1.561592 -0.168224 -1.839025\n",
       "b  1.069509  0.094385 -0.121773 -0.486491\n",
       "c  0.221530 -1.933896  1.088034 -0.098264\n",
       "d -0.929662 -0.100557 -1.776573 -0.671149\n",
       "e -0.213642 -0.635774  0.792030 -1.378112"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>a</th>\n",
       "      <th>b</th>\n",
       "      <th>c</th>\n",
       "      <th>d</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>0.279237</td>\n",
       "      <td>1.561592</td>\n",
       "      <td>-0.168224</td>\n",
       "      <td>-1.839025</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>1.069509</td>\n",
       "      <td>0.094385</td>\n",
       "      <td>-0.121773</td>\n",
       "      <td>-0.486491</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>0.221530</td>\n",
       "      <td>-1.933896</td>\n",
       "      <td>1.088034</td>\n",
       "      <td>-0.098264</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>d</th>\n",
       "      <td>-0.929662</td>\n",
       "      <td>-0.100557</td>\n",
       "      <td>-1.776573</td>\n",
       "      <td>-0.671149</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>e</th>\n",
       "      <td>-0.213642</td>\n",
       "      <td>-0.635774</td>\n",
       "      <td>0.792030</td>\n",
       "      <td>-1.378112</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 16
  },
  {
   "cell_type": "code",
   "source": [
    "\n",
    "# DataFrame，iloc是前闭后开[)\n",
    "print(df_obj)\n",
    "print('-' * 50)\n",
    "print(df_obj.iloc[0:2, 0:2])\n",
    "print('-' * 50)\n",
    "print(df_obj.iloc[[0, 2], [0, 2]])  # 不连续索引\n",
    "print('-' * 50)\n",
    "print(df_obj.iloc[0, 0])  # 取一个值\n",
    "print(df_obj.iloc[[0], [0]])  #得到一个DataFrame"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-01-07T12:14:07.734624Z",
     "start_time": "2025-01-07T12:14:07.725138Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          a         b         c         d\n",
      "a  0.279237  1.561592 -0.168224 -1.839025\n",
      "b  1.069509  0.094385 -0.121773 -0.486491\n",
      "c  0.221530 -1.933896  1.088034 -0.098264\n",
      "d -0.929662 -0.100557 -1.776573 -0.671149\n",
      "e -0.213642 -0.635774  0.792030 -1.378112\n",
      "--------------------------------------------------\n",
      "          a         b\n",
      "a  0.279237  1.561592\n",
      "b  1.069509  0.094385\n",
      "--------------------------------------------------\n",
      "          a         c\n",
      "a  0.279237 -0.168224\n",
      "c  0.221530  1.088034\n",
      "--------------------------------------------------\n",
      "0.2792370643437556\n",
      "          a\n",
      "a  0.279237\n"
     ]
    }
   ],
   "execution_count": 17
  },
  {
   "cell_type": "code",
   "source": [
    "#没有设置行和列索引的DataFrame，位置索引iloc和标签索引loc的区别\n",
    "df_obj2 = pd.DataFrame(np.random.randn(5, 4))\n",
    "print(df_obj2)\n",
    "print('-' * 50)\n",
    "print(df_obj2.iloc[0:2])  #左闭右开 2行\n",
    "print('-' * 50)\n",
    "print(df_obj2.loc[0:2])  #左闭右闭 3行"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-07T12:14:10.375364Z",
     "start_time": "2025-01-07T12:14:10.367124Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          0         1         2         3\n",
      "0  0.070255 -0.992153  0.379707 -0.275874\n",
      "1  0.355631 -1.049138 -0.433034  0.259484\n",
      "2  0.102394  0.755435 -0.503330 -2.515792\n",
      "3 -0.492261  0.517484  0.126659  0.692850\n",
      "4 -0.350444  0.075865  0.655276  0.150669\n",
      "--------------------------------------------------\n",
      "          0         1         2         3\n",
      "0  0.070255 -0.992153  0.379707 -0.275874\n",
      "1  0.355631 -1.049138 -0.433034  0.259484\n",
      "--------------------------------------------------\n",
      "          0         1         2         3\n",
      "0  0.070255 -0.992153  0.379707 -0.275874\n",
      "1  0.355631 -1.049138 -0.433034  0.259484\n",
      "2  0.102394  0.755435 -0.503330 -2.515792\n"
     ]
    }
   ],
   "execution_count": 18
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 5.对齐运算"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "source": [
    "import pandas as pd\n",
    "\n",
    "s1 = pd.Series(range(10, 20), index=range(10))\n",
    "s2 = pd.Series(range(20, 25), index=range(5))\n",
    "# Series 对齐运算\n",
    "print('s1+s2: ')\n",
    "s3 = s1 + s2\n",
    "print(s3)  #缺失数据默认是NaN  np.nan"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-01-07T12:14:14.771342Z",
     "start_time": "2025-01-07T12:14:14.766293Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "s1+s2: \n",
      "0    30.0\n",
      "1    32.0\n",
      "2    34.0\n",
      "3    36.0\n",
      "4    38.0\n",
      "5     NaN\n",
      "6     NaN\n",
      "7     NaN\n",
      "8     NaN\n",
      "9     NaN\n",
      "dtype: float64\n"
     ]
    }
   ],
   "execution_count": 19
  },
  {
   "cell_type": "code",
   "source": [
    "#两个长度不同的一维ndarray相加\n",
    "a1 = np.array([1, 2, 3, 4, 5])\n",
    "a2 = np.array([1])  # 长度为1\n",
    "print(a2.shape)\n",
    "print(a1 + a2)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-07T12:14:17.978125Z",
     "start_time": "2025-01-07T12:14:17.974059Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1,)\n",
      "[2 3 4 5 6]\n"
     ]
    }
   ],
   "execution_count": 20
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T12:14:33.700238Z",
     "start_time": "2025-01-07T12:14:33.695680Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print(s2)\n",
    "s1"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    20\n",
      "1    21\n",
      "2    22\n",
      "3    23\n",
      "4    24\n",
      "dtype: int64\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "0    10\n",
       "1    11\n",
       "2    12\n",
       "3    13\n",
       "4    14\n",
       "5    15\n",
       "6    16\n",
       "7    17\n",
       "8    18\n",
       "9    19\n",
       "dtype: int64"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 21
  },
  {
   "cell_type": "code",
   "source": [
    "print(np.isnan(s3[6]))\n",
    "print('-' * 50)\n",
    "print(s2.add(s1, fill_value=0))  #未对齐的数据将和填充值做运算\n",
    "print(s2.sub(s1, fill_value=0))  #s2-s1"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-07T12:14:54.058494Z",
     "start_time": "2025-01-07T12:14:54.050951Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "--------------------------------------------------\n",
      "0    30.0\n",
      "1    32.0\n",
      "2    34.0\n",
      "3    36.0\n",
      "4    38.0\n",
      "5    15.0\n",
      "6    16.0\n",
      "7    17.0\n",
      "8    18.0\n",
      "9    19.0\n",
      "dtype: float64\n",
      "0    10.0\n",
      "1    10.0\n",
      "2    10.0\n",
      "3    10.0\n",
      "4    10.0\n",
      "5   -15.0\n",
      "6   -16.0\n",
      "7   -17.0\n",
      "8   -18.0\n",
      "9   -19.0\n",
      "dtype: float64\n"
     ]
    }
   ],
   "execution_count": 22
  },
  {
   "cell_type": "code",
   "source": [
    "#df的对齐运算\n",
    "import numpy as np\n",
    "\n",
    "df1 = pd.DataFrame(np.ones((2, 2)), columns=['a', 'b'])\n",
    "df2 = pd.DataFrame(np.ones((3, 3)), columns=['a', 'b', 'c'])\n",
    "print(df1)\n",
    "print(df2)\n",
    "print('-' * 50)\n",
    "print(df2.dtypes)\n",
    "print(df1 - df2)\n",
    "print(df2.sub(df1, fill_value=2))  #未对齐的数据将和填充值做运算"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    },
    "ExecuteTime": {
     "end_time": "2025-01-07T12:16:14.537232Z",
     "start_time": "2025-01-07T12:16:14.525776Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "     a    b\n",
      "0  1.0  1.0\n",
      "1  1.0  1.0\n",
      "     a    b    c\n",
      "0  1.0  1.0  1.0\n",
      "1  1.0  1.0  1.0\n",
      "2  1.0  1.0  1.0\n",
      "--------------------------------------------------\n",
      "a    float64\n",
      "b    float64\n",
      "c    float64\n",
      "dtype: object\n",
      "     a    b   c\n",
      "0  0.0  0.0 NaN\n",
      "1  0.0  0.0 NaN\n",
      "2  NaN  NaN NaN\n",
      "     a    b    c\n",
      "0  0.0  0.0 -1.0\n",
      "1  0.0  0.0 -1.0\n",
      "2 -1.0 -1.0 -1.0\n"
     ]
    }
   ],
   "execution_count": 23
  },
  {
   "cell_type": "markdown",
   "source": [],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 总结：没对齐的元素，默认填充NaN，对齐运算时，fill_value参数可以指定填充值。"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
